package com.zhkj.txg.module.member.ui

import android.view.View
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.zhkj.lib.base.ViewModelActivity
import com.zhkj.lib.http.HttpResponseStatus
import com.zhkj.txg.R
import com.zhkj.txg.module.member.adapter.MemberOrderAdapter
import com.zhkj.txg.module.member.vm.MemberListViewModel
import com.zhkj.txg.module.order.popup.OrderReceivePopupWindow
import kotlinx.android.synthetic.main.activity_member_order.*

class MemberOrderListActivity : ViewModelActivity<MemberListViewModel>() {
    override fun setViewModel(): MemberListViewModel = createViewModel()
    override fun statusTitleBar(): View? = titleMember
    override val layoutResId: Int = R.layout.activity_member_order

    private val orderAdapter = MemberOrderAdapter()

    // 确认收货的popup
    private val receivePopupWindow: OrderReceivePopupWindow by lazy {
        OrderReceivePopupWindow(this) { _, orderId ->
            viewModel.memberReceive(orderId)
        }
    }

    override fun initUI() {
        with(rvMemberOrder) {
            layoutManager = LinearLayoutManager(this@MemberOrderListActivity)
            adapter = orderAdapter
        }
        with(orderAdapter) {
            addChildClickViewIds(R.id.tvMemberOrderReceive)
            setEmptyView(R.layout.layout_empty_data)
            setOnItemClickListener { _, _, position ->
                MemberOrderDetailActivity.open(this@MemberOrderListActivity, data[position].id)
            }
            setOnItemChildClickListener { _, view, position ->
                when (view.id) {
                    R.id.tvMemberOrderReceive -> {
                        receivePopupWindow.setData(0, data[position].id)
                        receivePopupWindow.showPopupWindow()
                    }
                }
            }
        }
        refreshMember.setOnRefreshListener { initData() }
    }

    override fun initData() {
        viewModel.orderList(true)
    }

    override fun initViewModel() {
        viewModel.orderListLiveData.observe(this, Observer { response ->
            when (response.status) {
                HttpResponseStatus.Status.START -> Unit
                HttpResponseStatus.Status.SUCCESS -> response.response?.let {
                    // 判断加载状态
                    if (!it.loadMore) refreshMember.finishRefresh()
                    else if (it.hasMore) orderAdapter.loadMoreModule?.loadMoreComplete()
                    else orderAdapter.loadMoreModule?.loadMoreEnd()
                    // 数据填充
                    if (it.loadMore) orderAdapter.addData(it.data)
                    else orderAdapter.setNewData(it.data)
                }
                HttpResponseStatus.Status.FAILURE -> {
                    response.exception?.printStackTrace()
                    toast(response.statusTip)
                    if (viewModel.page > 1) orderAdapter.loadMoreModule?.loadMoreFail()
                    else refreshMember.finishRefresh(false)
                }
            }
        })
        viewModel.memberReceiveLiveData.observe(this, Observer {
            handlerResponseStatus(it.status, it.statusTip)
            if (it.status == HttpResponseStatus.Status.SUCCESS) initData()
        })
    }
}